
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.IO;
using System.Text;
//using com.ddsc.database;
using DDSC.OracleADO;
using com.ddsc.common;
using SMAS;
using System.Resources;
using System.Data.OracleClient;

namespace smasb2b
{
	public class VNDW0061 : BasePage
	{
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected System.Web.UI.WebControls.Label lblINVOICES_NO;
		protected System.Web.UI.WebControls.Label lblINVOICES_DATE;
		protected System.Web.UI.WebControls.Label lblINVOICES_AMT;
		protected ddscControlLibrary.SubmitButtonCtrl btnExit;
		protected System.Web.UI.HtmlControls.HtmlImage IMG1;
		protected ddscControlLibrary.DateCtrl txtINVOICES_DATE;
		protected ddscControlLibrary.InputBoxCtrl INVOICES_AMT;
		protected ddscControlLibrary.SubmitButtonCtrl btnSave;
		protected ddscControlLibrary.SubmitButtonCtrl btnDel;
		protected ddscControlLibrary.InputBoxCtrl txtINVOICES_NO;
		protected ddscControlLibrary.InputBoxCtrl txtINVOICES_AMT;
		protected System.Web.UI.WebControls.Label lblINVOICES_NO_ERR;
		protected System.Web.UI.WebControls.Label lblINVOICES_DATE_ERR;
		protected System.Web.UI.WebControls.Label lblINVOICES_AMT_ERR;
		protected System.Web.UI.WebControls.Label lblCURRENCY_TYPE;
		protected System.Web.UI.WebControls.DropDownList drpCURRENCY_TYPE;
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		
		private bool getCURRENCY_TYPE()
		{
			bool result = false;
			int i;
			try
			{
				// 將資料寫入下拉式選單
				DataSet ds = drpCURRENCY_TYPE_NO();
				if (!(ds == null) && ds.Tables.Count > 0)
				{
					if (!(ds == null) && ds.Tables[0].Rows[0]["ReturnCode"] != "100" && ds.Tables[0].Rows[0]["ReturnCode"] != "999")
					{
						drpCURRENCY_TYPE.DataSource = ds.Tables["BAS_CURRENCY"];
						drpCURRENCY_TYPE.DataTextField = "DESCRIPTION";
						drpCURRENCY_TYPE.DataValueField = "CURRENCY_TYPE";
						drpCURRENCY_TYPE.DataBind();
					}
					else
					{
						drpCURRENCY_TYPE.Items.Insert(0, new ListItem("", ""));
					}
				}
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			finally
			{
			}
			return result;
		}
		
		public DataSet drpCURRENCY_TYPE_NO()
		{
			DataSet dsReturn = default(DataSet);
			try
			{
				StringBuilder sb = new StringBuilder();
				sb.Append("select CURRENCY_TYPE,(CURRENCY_TYPE||\'-\'||DESCRIPTION)DESCRIPTION from BAS_CURRENCY  order by CURRENCY_TYPE  ");
				string dtName = "BAS_CURRENCY";
				string sqlCommands = sb.ToString();
				dbAccess dba = new dbAccess();
				dsReturn = dba.runExecuteQuery(dtName, sqlCommands);
			}
			catch (Exception ex)
			{
				dsReturn = new DataSet("BAS_CURRENCY");
				dsReturn.Tables.Add(createErrorTable(ex.Message));
			}
			return dsReturn;
		}
		
		
		private DataTable createErrorTable(string mError)
		{
			
			dbReturnInfo dbInfo = new dbReturnInfo("info");
			dbInfo.AddNewRow();
			dbInfo.setReturnDataValue("F", System.Convert.ToString(999), 0, mError);
			return dbInfo.getReturnDataTable();
		}
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			//在這裡放置使用者程式碼以初始化網頁
            this.btnSave.Click+=btnSAVE_Click;
            this.btnDel.Click += this.btnDEL_Click;
			try
			{
				
				if (!Page.IsPostBack)
				{
					getCURRENCY_TYPE();
					if ((string) (Page.Request["action_type"].ToUpper()) == "ADD")
					{
						this.btnDel.Visible = false;
                        //Adams:Mark
                        //txtINVOICES_NO.EnableView = true;
                        //txtINVOICES_DATE.EnableView = true;
					}
					else if ((string) (Page.Request["action_type"].ToUpper()) == "FIND")
					{
						this.btnDel.Visible = false;
						this.btnSave.Visible = false;
						txtINVOICES_NO.EnableView = true;
						txtINVOICES_DATE.EnableView = true;
						txtINVOICES_AMT.EnableView = true;
						drpCURRENCY_TYPE.Enabled = false;
					}
					else if ((string) (Page.Request["action_type"].ToUpper()) == "UPD")
					{
						this.btnDel.Visible = false;
                        txtINVOICES_NO.Enabled = false;                        
					}
					else if ((string) (Page.Request["action_type"].ToUpper()) == "DEL")
					{
						txtINVOICES_NO.EnableView = true;
						txtINVOICES_DATE.EnableView = true;
						txtINVOICES_AMT.EnableView = true;
						drpCURRENCY_TYPE.Enabled = false;
						this.btnSave.Visible = false;
					}
					else
					{
					}
					
					if ((((string) (Page.Request["action_type"].ToUpper()) == "FIND") || ((string) (Page.Request["action_type"].ToUpper()) == "UPD")) || ((string) (Page.Request["action_type"].ToUpper()) == "DEL"))
					{
						DataBase DataBase = new DataBase();
						DataSet ds = default(DataSet);
                        ArrayList m_arrParam = new ArrayList();
                        dbAccess dba = new dbAccess();
						string SelectCommand = "";
						string szINVOICES_NO = Page.Request["INVOICES_NO"];
						VENDOR LoginUser = GetLoginUser();
						string szVENDOR_NO = LoginUser.VENDOR_NO;

                        //Tony : 1.修改SQL語法使用參數，不組字串
						//SelectCommand = string.Format(" SELECT * FROM B2B_INVOICE WHERE INVOICES_NO=\'{0}\' AND VENDOR_NO=\'{1}\' ", szINVOICES_NO, szVENDOR_NO);
                        SelectCommand = " SELECT * FROM B2B_INVOICE WHERE INVOICES_NO=:szINVOICES_NO AND VENDOR_NO=:szVENDOR_NO ";

                        //Tony : 2.設定動態參數物件
                        m_arrParam.Add(dba.GetOracleParam(":szINVOICES_NO", szINVOICES_NO));
                        m_arrParam.Add(dba.GetOracleParam(":szVENDOR_NO", szVENDOR_NO));

                        //Tony : 3.修改執行方法Overload
						//ds = DataBase.Query_CLASS(SelectCommand, "B2B_INVOICE");
                        //ds = dba.runExecuteQuery("B2B_INVOICE", SelectCommand, m_arrParam);
                        //Ken20140715
                        ds = DataBase.Query_CLASS(SelectCommand, "B2B_INVOICE", m_arrParam);
						if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
						{
							this.txtINVOICES_NO.Text = System.Convert.ToString(ds.Tables["B2B_INVOICE"].Rows[0]["INVOICES_NO"].ToString());
							this.txtINVOICES_DATE.Text = System.Convert.ToString(ds.Tables["B2B_INVOICE"].Rows[0]["INVOICES_DATE"].ToString());
							this.txtINVOICES_AMT.Text = System.Convert.ToString(ds.Tables["B2B_INVOICE"].Rows[0]["INVOICES_AMT"].ToString());
							this.drpCURRENCY_TYPE.SelectedValue = System.Convert.ToString(ds.Tables["B2B_INVOICE"].Rows[0]["CURRENCY_TYPE"].ToString());
						}
						else
						{
							showException("對不起! 此筆資料，已被刪除");
						}
						
					}
					
					
				}
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		private void btnSAVE_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				string action_type = this.Request["action_type"].ToString();
				if (action_type == "ADD")
				{
					addB2B_INVOICE();
				}
				else
				{
					updB2B_INVOICE();
				}
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			
		}
		
		private void btnDEL_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				delB2B_INVOICE();
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		private void updB2B_INVOICE()
		{
			// 設定傳送的 DataSet 物件
            //Tony:use common
			//ddscDataSet ddscDs = new ddscDataSet();
            com.ddsc.common.ddscDataSet ddscDs = new com.ddsc.common.ddscDataSet();

			VENDOR LoginUser = GetLoginUser();
			
			// 宣告回傳DataSet
			DataSet dsResult = new DataSet();
			try
			{
				//基本資料

                //Tony:use common
				//ddscDataTable ddscDt = ddscDs.createDataTable("B2B_INVOICE");
                com.ddsc.common.ddscDataTable ddscDt = ddscDs.createDataTable("B2B_INVOICE");

				// 新增Row至ddscDt
				ddscDt.addNewRow();
				// 設定dscDt各欄位Value，參數一：columnName,參數二：columnValue
				ddscDt.setColumnValue("action", "string", "upd");
				ddscDt.setColumnValue("INVOICES_NO", "string", txtINVOICES_NO.Text.Trim());
				ddscDt.setColumnValue("INVOICES_DATE", "string", txtINVOICES_DATE.Text.Trim());
				ddscDt.setColumnValue("INVOICES_AMT", "string", txtINVOICES_AMT.Text.Trim());
				ddscDt.setColumnValue("VENDOR_NO", "string", LoginUser.VENDOR_NO);
				ddscDt.setColumnValue("CURRENCY_TYPE", "string", drpCURRENCY_TYPE.SelectedValue);
				
				
				ddscDt.setColumnValue("status", "string", "1");
				ddscDt.setColumnValue("upd_id", "string", LoginUser.VENDOR_NO);
				ddscDt.setColumnValue("upd_date", "string", SystemDate);
				ddscDt.setColumnValue("upd_time", "string", SystemTime);
                ddscDt.setWhereValue("INVOICES_NO", txtINVOICES_NO.Text.Trim());
                //ddscDt.setWhereValue("VENDOR_NO",  LoginUser.VENDOR_NO);
				ddscDs.addDataTable(ddscDt);
				
				// 執行新增method
				DataBase DataBase = new DataBase();
				dsResult = DataBase.Addition(ddscDs, "", (System.Array) null, (System.Array) null);
				// 寫入script至client端
				regScript(dsResult, "upd");
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			finally
			{
			}
		}
		
		private void delB2B_INVOICE()
		{
            //Tony:use common
            //ddscDataSet ddscDs = new ddscDataSet();
            com.ddsc.common.ddscDataSet ddscDs = new com.ddsc.common.ddscDataSet();
			
			VENDOR LoginUser = GetLoginUser();
			
			// 宣告回傳DataSet
			DataSet dsResult = new DataSet();
			try
			{
				//基本資料
                //Tony:use common
				//ddscDataTable ddscDt = ddscDs.createDataTable("B2B_INVOICE");
                com.ddsc.common.ddscDataTable ddscDt = ddscDs.createDataTable("B2B_INVOICE");

				// 新增Row至ddscDt
				ddscDt.addNewRow();
				// 設定dscDt各欄位Value，參數一：columnName,參數二：columnValue
				ddscDt.setColumnValue("action", "string", "upd");
				ddscDt.setColumnValue("INVOICES_NO", "string", txtINVOICES_NO.Text.Trim());
				ddscDt.setColumnValue("INVOICES_DATE", "string", txtINVOICES_DATE.Text.Trim());
				ddscDt.setColumnValue("INVOICES_AMT", "string", txtINVOICES_AMT.Text.Trim());
				
				
				ddscDt.setColumnValue("status", "string", "0");
				ddscDt.setColumnValue("upd_id", "string", LoginUser.VENDOR_NO);
				ddscDt.setColumnValue("upd_date", "string", SystemDate);
				ddscDt.setColumnValue("upd_time", "string", SystemTime);
				ddscDt.setWhereValue("INVOICES_NO", txtINVOICES_NO.Text.Trim());
				
				
				ddscDt.addNewRow();
				ddscDt.setColumnValue("action", "string", "del");
				ddscDt.setColumnValue("INVOICES_NO", "string", txtINVOICES_NO.Text.Trim());
				ddscDt.setWhereValue("INVOICES_NO", txtINVOICES_NO.Text.Trim());
				ddscDs.addDataTable(ddscDt);
				
				// 執行新增method
				DataBase DataBase = new DataBase();
				dsResult = DataBase.Addition(ddscDs, "", (System.Array) null, (System.Array) null);
				// 寫入script至client端
				regScript(dsResult, "del");
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			finally
			{
			}
			
		}
		
		private void addB2B_INVOICE()
		{
            //Tony:use common
            //ddscDataSet ddscDs = new ddscDataSet();
            com.ddsc.common.ddscDataSet ddscDs = new com.ddsc.common.ddscDataSet();
			
			VENDOR LoginUser = GetLoginUser();
			
			// 宣告回傳DataSet
			DataSet dsResult = new DataSet();
			try
			{
				//基本資料
                //Tony:use common
				//ddscDataTable ddscDt = ddscDs.createDataTable("B2B_INVOICE");
                com.ddsc.common.ddscDataTable ddscDt = ddscDs.createDataTable("B2B_INVOICE");

				// 新增Row至ddscDt
				ddscDt.addNewRow();
				// 設定dscDt各欄位Value，參數一：columnName,參數二：columnValue
				ddscDt.setColumnValue("action", "string", "add");
				ddscDt.setColumnValue("INVOICES_NO", "string", txtINVOICES_NO.Text.Trim());
				ddscDt.setColumnValue("INVOICES_DATE", "string", txtINVOICES_DATE.Text.Trim());
				ddscDt.setColumnValue("INVOICES_AMT", "string", txtINVOICES_AMT.Text.Trim());
				ddscDt.setColumnValue("VENDOR_NO", "string", LoginUser.VENDOR_NO);
				ddscDt.setColumnValue("CURRENCY_TYPE", "string", drpCURRENCY_TYPE.SelectedValue);
				
				
				ddscDt.setColumnValue("status", "string", "1");
				ddscDt.setColumnValue("crt_id", "string", LoginUser.VENDOR_NO);
				ddscDt.setColumnValue("crt_date", "string", SystemDate);
				ddscDt.setColumnValue("crt_time", "string", SystemTime);
				ddscDt.setColumnValue("upd_id", "string", LoginUser.VENDOR_NO);
				ddscDt.setColumnValue("upd_date", "string", SystemDate);
				ddscDt.setColumnValue("upd_time", "string", SystemTime);
				ddscDt.setWhereValue("INVOICES_NO", txtINVOICES_NO.Text.Trim());
				ddscDs.addDataTable(ddscDt);
				
				// 執行新增method
				DataBase DataBase = new DataBase();
				dsResult = DataBase.Addition(ddscDs, "", (System.Array) null, (System.Array) null);
				// 寫入script至client端
				regScript(dsResult, "add");
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
			finally
			{
			}
		}
		
		private void CLEAN()
		{
			txtINVOICES_AMT.Text = "";
			txtINVOICES_NO.Text = "";
			
		}
	}
	
}
